package com.chengqj.blogsso.controller;

import com.chengqj.blogsso.constant.Consts;
import com.chengqj.user.server.dto.*;
import com.chengqj.user.server.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户登陆类")
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @PostMapping(value = "/login")
    @ApiOperation(value = "登陆接口", httpMethod = "POST")
    public UserLoginResponse login(@ApiParam(name = "userName", value = "用户名", required = true) String userName,@ApiParam(name = "password", value = "密码", required = true) String password){
        UserLoginResponse response = new UserLoginResponse();
        if (StringUtils.isEmpty(userName)){
            response.setCode(Consts.ReturnCode.PARAM_ERROR);
            response.setMsg(Consts.ReturnMsg.USERNAME_EMPTY);
            return response;
        }
        if (StringUtils.isEmpty(password)){
            response.setCode(Consts.ReturnCode.PARAM_ERROR);
            response.setMsg(Consts.ReturnMsg.PASSWORD_EMPTY);
            return response;
        }
        UserLoginRequest request = new UserLoginRequest();
        request.setUserName(userName);
        request.setPassword(password);
        UserLoginResponse loginResponse = userService.login(request);
        return loginResponse;
    }

    @GetMapping(value = "/checkAuth/{token}")
    @ApiOperation(value = "校验token接口", httpMethod = "GET")
    public CheckAuthResponse checkAuth(@ApiParam(name = "token", value = "登陆凭证", required = true) @PathVariable String token){
        if (StringUtils.isEmpty(token)){
            CheckAuthResponse response = new CheckAuthResponse();
            response.setCode(Consts.ReturnCode.TOKEN_ERROR);
            response.setMsg(Consts.ReturnMsg.TOKEN_EMPTY);
            return response;
        }
        CheckAuthRequest request = new CheckAuthRequest();
        request.setToken(token);
        CheckAuthResponse response = userService.checkAuth(request);
        return response;
    }
    @PostMapping(value = "/register")
    @ApiOperation(value = "注册接口", httpMethod = "POST")
    public UserRegisterResponse register(@ApiParam(name = "userName", value = "用户名", required = true)String userName,@ApiParam(name = "password", value = "密码", required = true)String password){
        UserRegisterResponse response = new UserRegisterResponse();
        if (StringUtils.isEmpty(userName)){
            response.setCode(Consts.ReturnCode.PARAM_ERROR);
            response.setMsg(Consts.ReturnMsg.USERNAME_EMPTY);
            return response;
        }
        if (StringUtils.isEmpty(password)){
            response.setCode(Consts.ReturnCode.PARAM_ERROR);
            response.setMsg(Consts.ReturnMsg.PASSWORD_EMPTY);
            return response;
        }
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setPassword(password);
        response = userService.register(userRegisterRequest);
        return response;
    }

}
